package com.king.armips;

import android.text.TextUtils;

import java.io.File;

public class ArmipsDroid {

    // Used to load the 'armips' library on application startup.
    static {
        System.loadLibrary("armips");
    }

    private native String[] execute(int symFileVersion,
                               boolean errorOnWarning,
                               boolean silent,
                               boolean showStats,
                               String[] equNameList,
                               String[] equValueList,
                               String[] labelNameList,
                               long[] labelValueList,
                               String inputFileName,
                               String tempFileName,
                               String symFileName,
                               boolean useAbsoluteFileNames,
                               String rootDir
    );

    public String runArmips(ArmipsArguments arguments){
        if (arguments == null){
            return "Cannot parse arguments; aborting.";
        }
        if (arguments.inputFileName == null || arguments.inputFileName.isEmpty()){
            return "Missing input assembly file";
        }
        File inputFile;
        if (TextUtils.isEmpty(arguments.rootDir)){
            inputFile = new File(arguments.inputFileName);
        }
        else {
            inputFile = new File(arguments.rootDir, arguments.inputFileName);
        }
        if (!inputFile.exists()){
            return "File \""+arguments.inputFileName+"\" not found";
        }

        String[] equNameList = new String[arguments.equList.size()];
        String[] equValueList = new String[arguments.equList.size()];
        for (int i = 0; i < arguments.equList.size(); i++) {
            ArmipsArguments.EquationDefinition equationDefinition = arguments.equList.get(i);
            equNameList[i] = equationDefinition.name;
            equValueList[i] = equationDefinition.value;
        }

        String[] labelNameList = new String[arguments.labels.size()];
        long[] labelValueList = new long[arguments.labels.size()];
        for (int i = 0; i < arguments.labels.size(); i++) {
            ArmipsArguments.LabelDefinition labelDefinition = arguments.labels.get(i);
            labelNameList[i] = labelDefinition.name;
            labelValueList[i] = labelDefinition.value;
        }

        String[] resultC = execute(arguments.symFileVersion,
                arguments.errorOnWarning,
                arguments.silent,
                arguments.showStats,
                equNameList,
                equValueList,
                labelNameList,
                labelValueList,
                arguments.inputFileName,
                arguments.tempFileName,
                arguments.symFileName,
                arguments.useAbsoluteFileNames,
                arguments.rootDir
        );

        StringBuilder resultBuilder = new StringBuilder();
        if (resultC != null){
            for (String s : resultC) {
                if (resultBuilder.length() > 0)
                    resultBuilder.append("\n");
                resultBuilder.append(s);
            }
        }

        return resultBuilder.toString();
    }

}
